<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="申请人" prop="approveName">
        <el-input
          v-model="queryParams.approveName"
          placeholder="请输入申请人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请金额" prop="amount">
        <el-input
          v-model="queryParams.amount"
          placeholder="请输入申请金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="auditStatus">
        <el-select
          v-model="queryParams.auditStatus"
          placeholder="请选择审核状态"
        >
          <el-option
            v-for="item in shList"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="转账状态" prop="transferStatus">
        <el-select
          v-model="queryParams.transferStatus"
          placeholder="请选择转账状态"
        >
          <el-option
            v-for="item in zhList"
            :key="item.value"
            :label="item.name"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" v-if="adminRoles === 'school'">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:approve:add']"
          >新增</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="approveList"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="申请人" align="center" prop="approveName" />
      <el-table-column label="申请金额" align="center" prop="amount" />
      <el-table-column label="申请理由" align="center" prop="approveRemark" />
      <el-table-column label="审批人" align="center" prop="auditName" />
      <el-table-column label="审核理由" align="center" prop="auditRemark" />
      <el-table-column label="转账人" align="center" prop="transferName" />
      <el-table-column label="转账理由" align="center" prop="transferRemark" />
      <el-table-column
        label="转账时间"
        align="center"
        prop="payTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.transferTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请状态" align="center" prop="auditStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.auditStatus == 0">待审核</span>
          <span v-if="scope.row.auditStatus == 1">已通过</span>
          <span v-if="scope.row.auditStatus == 2">已拒绝</span>
        </template>
      </el-table-column>
      <el-table-column label="转账状态" align="center" prop="transferStatus">
        <template slot-scope="scope">
          <span v-if="scope.row.transferStatus == 0">待转账</span>
          <span v-if="scope.row.transferStatus == 1">已转账</span>
          <span v-if="scope.row.transferStatus == 2">已拒绝</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="adminRoles === 'school' && scope.row.transferStatus == 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            v-if="adminRoles === 'school' && scope.row.transferStatus == 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
          <el-button
            v-if="adminRoles == 'jiaoyuju' && scope.row.auditStatus == 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlesuccess(scope.row, 1)"
            >通过</el-button
          >
          <el-button
            v-if="adminRoles == 'jiaoyuju' && scope.row.auditStatus == 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlesuccess(scope.row, 2)"
            >拒绝</el-button
          >
          <el-button
            v-if="
              adminRoles == 'bank' &&
              scope.row.auditStatus == 1 &&
              scope.row.transferStatus == 0
            "
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleZh(scope.row, 1)"
            >转账</el-button
          >
          <el-button
            v-if="
              adminRoles == 'bank' &&
              scope.row.auditStatus == 1 &&
              scope.row.transferStatus == 0
            "
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleZh(scope.row, 2)"
            >拒绝</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改审批管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="申请金额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入申请金额" />
        </el-form-item>
        <el-form-item label="申请理由" prop="approveRemark">
          <el-input
            v-model="form.approveRemark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 审核 -->
    <el-dialog title="审核" :visible.sync="shopen" width="700px" append-to-body>
      <el-form ref="shform" :model="shform" :rules="shrules" label-width="80px">
        <el-form-item label="审核理由" prop="auditRemark">
          <el-input
            v-model="shform.auditRemark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitSh">确 定</el-button>
        <el-button @click="cancelSh">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 转账 -->
    <el-dialog title="转账" :visible.sync="zhopen" width="700px" append-to-body>
      <el-form ref="zhform" :model="zhform" :rules="zhrules" label-width="80px">
        <el-form-item label="转账理由" prop="transferRemark">
          <el-input
            v-model="zhform.transferRemark"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitZh">确 定</el-button>
        <el-button @click="cancelZh">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listApprove,
  getApprove,
  delApprove,
  addApprove,
  updateApprove,
  auditApprove,
  transferApprove,
} from "@/api/business/approve";

export default {
  name: "Approve",
  data() {
    return {
      shList: [
        { name: "待审核", value: 0 },
        { name: "已通过", value: 1 },
        { name: "已拒绝", value: 2 },
      ],
      zhList: [
        { name: "待转账", value: 0 },
        { name: "已转账", value: 1 },
        { name: "已拒绝", value: 2 },
      ],
      shform: {},
      shopen: false,
      shrules: {
        auditRemark: [
          { required: true, message: "审核理由不能为空", trigger: "blur" },
        ],
      },

      zhform: {},
      zhopen: false,
      zhrules: {
        transferRemark: [
          { required: true, message: "转账理由不能为空", trigger: "blur" },
        ],
      },
      adminRoles: "",
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 审批管理表格数据
      approveList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        approveUserId: null,
        auditUserId: null,
        userId: null,
        approveRemark: null,
        auditRemark: null,
        transferRemark: null,
        code: null,
        amount: null,
        payTime: null,
        auditStatus: null,
        transferStatus: null,
        approveName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        approveRemark: [
          { required: true, message: "申请理由不能为空", trigger: "blur" },
        ],

        amount: [
          { required: true, message: "申请金额不能为空", trigger: "blur" },
        ],
      },
    };
  },

  created() {
    console.log(this.$store.state.user.roles);
    if (this.$store.state?.user?.roles) {
      this.adminRoles = this.$store.state?.user?.roles[0];
    }
    this.getList();
  },
  methods: {
    /** 查询审批管理列表 */
    getList() {
      this.loading = true;
      listApprove(this.queryParams).then((response) => {
        this.approveList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        approveUserId: null,
        auditUserId: null,
        userId: null,
        approveRemark: null,
        auditRemark: null,
        transferRemark: null,
        code: null,
        amount: null,
        payTime: null,
        auditStatus: "0",
        transferStatus: "0",
      };
      this.resetForm("form");
    },
    Shrest() {
      this.shform = {
        id: null,
        auditRemark: null,
        auditStatus: "0",
      };
      this.resetForm("shform");
    },
    cancelSh() {
      this.Shrest();
      this.shopen = false;
    },
    submitSh() {
      this.$refs["shform"].validate((valid) => {
        if (valid) {
          auditApprove(this.shform).then((response) => {
            // this.$modal.msgSuccess("修改成功");
            this.shopen = false;
            this.getList();
          });
        }
      });
    },

    Zhrest() {
      this.zhform = {
        id: null,
        transferRemark: null,
        transferStatus: "0",
      };
      this.resetForm("zhform");
    },
    cancelZh() {
      this.Zhrest();
      this.zhopen = false;
    },
    submitZh() {
      this.$refs["zhform"].validate((valid) => {
        if (valid) {
          transferApprove(this.zhform).then((response) => {
            // this.$modal.msgSuccess("修改成功");
            this.zhopen = false;
            this.getList();
          });
        }
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加审批管理";
    },
    handlesuccess(row, status) {
      this.Shrest();
      this.shform.id = row.id;
      this.shform.auditStatus = status;
      this.shopen = true;
    },
    handleZh(row, status) {
      this.Zhrest();
      this.zhform.id = row.id;
      this.zhform.transferStatus = status;
      this.zhopen = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getApprove(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改审批管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateApprove(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApprove(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除审批管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delApprove(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "business/approve/export",
        {
          ...this.queryParams,
        },
        `approve_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
